package edu.unika.aifb.graphindex;

import java.util.Scanner;
import java.util.Set;

import joptsimple.OptionParser;
import joptsimple.OptionSet;
import edu.unika.aifb.graphindex.index.IndexDirectory;
import edu.unika.aifb.graphindex.index.IndexReader;
import edu.unika.aifb.graphindex.query.KeywordQuery;
import edu.unika.aifb.graphindex.searcher.keyword.evaluation.Row;
import edu.unika.aifb.graphindex.searcher.keyword.evaluation.TopKQueryEvaluator;

public class ConsoleApp {
	
	public static void main(String[] args) throws Exception{
		OptionParser op = new OptionParser();
		op.accepts("id", "index directory").withRequiredArg().ofType(String.class).describedAs("index directory");
		op.accepts("nq", "number of generated queries").withRequiredArg().ofType(String.class).describedAs("number of generated queries");
		op.accepts("nr", "number of results per query").withRequiredArg().ofType(String.class).describedAs("number of results per query");
		
		OptionSet os = op.parse(args);
		
		if (!os.has("id") || !os.has("nq") || !os.has("nr")) {
			op.printHelpOn(System.out);
			return;
		}

		String indexDirectory = (String)os.valueOf("id");
		int numberOfQueries = Integer.parseInt((String)os.valueOf("nq"));
		int numberOfResultsPerQuery = Integer.parseInt((String)os.valueOf("nr"));
		
		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter keyword query: ");
		
		String query = scanner.nextLine().trim();
		IndexReader ir = new IndexReader(new IndexDirectory(indexDirectory));
		TopKQueryEvaluator topKQueryEvaluator = new TopKQueryEvaluator(ir);
		
		KeywordQuery keywordQuery = new KeywordQuery("q1", query);
		Set<Row> rows =  topKQueryEvaluator.evaluate(keywordQuery, numberOfQueries , numberOfResultsPerQuery);
		
		for(Row row : rows)
			System.out.println(row.toString());
		
		ir.close();
	}
}
